<template>
  <div style="height: 100%;">
    <loading v-if="loadFlag"></loading>
  </div>
</template>

<script>
  import {Toast} from 'mint-ui'
  import loading from '../loading/loading'
  import {getQueryString, errcode, getOrSetSessionVal, getOrSetLocalVal} from '../../common/config'
  import {errcodeLogin} from '../../common/errorInfo'
  import {
    checkAuthorizeUrl,
    getToken,
    getUser,
    caseleibieall,
    sixcaseleibie,
    casequhua,
    unitpersonnel,
    getbioschildtype,
    getvideoschildtype,
    getfootschildtype,
    getfootsextractionmethod
  } from '../../api/api'
  export default {
    name: 'token',
    data () {
      return {
        loginNum: 0,
        loadFlag: true
      }
    },
    mounted () {
      this.checkCode()
    },
    components: {
      loading
    },
    methods: {
      checkCode () {
        // 获取code 没code 则 跳转
        const hash = window.location.hash
        const search = window.location.search
        const code = getQueryString(hash, 'code') || getQueryString(search, 'code')
        const idCard = window.localStorage.getItem('idCard') || ''
        if (!code && !idCard) {
          window.location.href = checkAuthorizeUrl()
          return
        }
        if (idCard) {
          this.$router.push({
            name: 'list'
          })
          return
        }
        this.getUserBy(code)
        // this.getUserBy('code')
      },
      getUserBy: async function (code) {
        const str = '成功'

        const v1 = await this.getUserByAsync()
        if (v1 !== str) {
          Toast({
            message: '获取登录信息失败,错误码:' + errcodeLogin.loginCode
          })
        }

        const v2 = await this.getDanweiInfoByAsync()
        if (v2 !== str) {
          Toast({
            message: '获取单位人员信息失败,错误码:' + errcodeLogin.companyUserInfo
          })
        }

        const v3 = await this.getAllCaseInfoByAsync()
        if (v3 !== str) {
          Toast({
            message: '获取全部案件类别信息失败,错误码:' + errcodeLogin.allCaseTypeInfo
          })
        }

        const v4 = await this.getSixCaseByAsync()
        if (v4 !== str) {
          Toast({
            message: '获取六类案件信息失败,错误码:' + errcodeLogin.sixCaseTypeInfo
          })
        }

        const v5 = await this.getCaseAreaByAsync()
        if (v5 !== str) {
          Toast({
            message: '获取案件区划信息失败,错误码:' + errcodeLogin.caseAreaInfo
          })
        }

        const v7 = await this.getbioschildtypeflag()
        if (v7 !== str) {
          Toast({
            message: '获取生物物证具体类型失败'
          })
        }

        const v8 = await this.getvideoschildtypeflag()
        if (v8 !== str) {
          Toast({
            message: '获取视听物证具体类型失败'
          })
        }

        const v9 = await this.getfootschildtypeflag()
        if (v9 !== str) {
          Toast({
            message: '获取足迹物证具体类型失败'
          })
        }

        const v10 = await this.getfootsextractionmethodflag()
        if (v10 !== str) {
          Toast({
            message: '获取足迹物证提取方法失败'
          })
        }

        const v6 = await this.getTokenByAsync(code)
        const v6Str = v6.split('-')[0] // 成功
        if (v6Str !== str) {
          Toast({
            message: '登陆失败,请联系管理员,错误码:' + errcodeLogin.token
          })
          return
        } else {
          const v6Code = v6.split('-')[1] // 身份证
          if (v6Code === '31010619761204162X') {
            window.localStorage.setItem('idCard', v6Code)
            window.location.href = '/test'
          }
        }

        this.$router.push({
          name: 'list'
        })
      },
      getTokenByAsync (code) {
        return new Promise((resolve, reject) => {
          getToken({
            code
          }, (res) => {
            if (res.data.code === errcode) {
              getOrSetSessionVal('SET', 'token', res.data.data.userToken)
              resolve('成功-' + res.data.data.userCard)
            }
            if (res.data.code === 600) {
              this.$router.push({
                name: 'error'
              })
              reject('失败')
            } else {
              /* eslint-disable */
              if (typeof (eshimin) === 'undefined') {
                return
              }
              /* eslint-disable */
              eshimin.closeWindow({
                onResponse: function (res) {
                  console.log('closeWindow res:' + res)
                }
              })
              reject('失败')
            }
          }, () => {
            reject('失败')
          })
        }).catch(err => {
          console.log(err)
        })
      },
      getUserByAsync () {
        return new Promise((resolve, reject) => {
          getUser({}, (res) => {
            /*
              organName: "上海市公安局刑事侦查总队"
              regionalism: "310000000000"
              techid: "8a8e981b1ca43085011ccfa4afdf3fe6"
              trueName: null
              userCard: "31010619761204162X"
              userName: "胡幸"
              userid: "8a8e981b1ca43085011ccfa4afdf3fe7"
             */
            const data = res.data.data
            if (data === null || data === [] || data === '') {
              reject('失败')
            }
            const USERINFO = {
              organName: data.organName,
              regionalism: data.regionalism,
              techid: data.techid,
              trueName: data.trueName,
              userCard: data.userCard,
              userName: data.userName,
              userid: data.userid
            }
            getOrSetSessionVal('SET', 'userInfo', JSON.stringify(USERINFO))
            resolve('成功')
          })
        }).catch(err => {
          console.log(err)
        })
      },
      getDanweiInfoByAsync () {
        // 单位人员
        return new Promise((resolve, reject) => {
          unitpersonnel({}, (ress) => {
            const res = ress.data
            if (res.code === errcode) {
              getOrSetSessionVal('SET', 'anjianunit', JSON.stringify(res.data))
              resolve('成功')
            }
            reject('失败')
          }, () => {
            reject('失败')
          })
        }).catch(err => {
          console.log(err)
        })
      },
      getAllCaseInfoByAsync () {
        // 全部案件类别
        return new Promise((resolve, reject) => {
          if (getOrSetLocalVal('GET', 'case_type_name')) {
            resolve('成功')
          } else {
            caseleibieall((ress) => {
              const res = ress.data
              if (res.code === errcode) {
                getOrSetLocalVal('SET', 'case_type_name', JSON.stringify(res.data))
                resolve('成功')
              }
              reject('失败')
            }, () => {
              reject('失败')
            })
          }
        }).catch(err => {
          console.log(err)
        })
      },
      getSixCaseByAsync () {
        // 六类案件
        return new Promise((resolve, reject) => {
          if (getOrSetLocalVal('GET', 'sixanjianleibie')) {
            resolve('成功')
          } else {
            sixcaseleibie((ress) => {
              const res = ress.data
              if (res.code === errcode) {
                getOrSetLocalVal('SET', 'sixanjianleibie', JSON.stringify(res.data))
                resolve('成功')
              }
              reject('失败')
            }, () => {
              reject('失败')
            })
          }
        }).catch(err => {
          console.log(err)
        })
      },
      getCaseAreaByAsync () {
        // 案件区划
        return new Promise((resolve, reject) => {
          if (getOrSetLocalVal('GET', 'regionalism_name')) {
            resolve('成功')
          } else {
            casequhua((ress) => {
              const res = ress.data
              if (res.code === errcode) {
                getOrSetLocalVal('SET', 'regionalism_name', JSON.stringify(res.data))
                resolve('成功')
              }
              reject('失败')
            }, () => {
              reject('失败')
            })
          }
        }).catch(err => {
          console.log(err)
        })
      },
      getbioschildtypeflag () {
        // 生物物证具体类型
        return new Promise((resolve, reject) => {
          if (getOrSetSessionVal('GET', 'bioschildtype')) {
            resolve('成功')
          } else {
            getbioschildtype((ress) => {
              const res = ress.data
              if (res.code === errcode) {
                getOrSetSessionVal('SET', 'bioschildtype', JSON.stringify(res.data))
                resolve('成功')
              }
              reject('失败')
            }, () => {
              reject('失败')
            })
          }
        }).catch(err => {
          console.log(err)
        })
      },
      getvideoschildtypeflag () {
        // 视听物证具体类型
        return new Promise((resolve, reject) => {
          if (getOrSetSessionVal('GET', 'videoschildtype')) {
            resolve('成功')
          } else {
            getvideoschildtype((ress) => {
              const res = ress.data
              if (res.code === errcode) {
                getOrSetSessionVal('SET', 'videoschildtype', JSON.stringify(res.data))
                resolve('成功')
              }
              reject('失败')
            }, () => {
              reject('失败')
            })
          }
        }).catch(err => {
          console.log(err)
        })
      },
      getfootschildtypeflag () {
        // 足迹物证具体类型
        return new Promise((resolve, reject) => {
          if (getOrSetSessionVal('GET', 'footschildtype')) {
            resolve('成功')
          } else {
            getfootschildtype((ress) => {
              const res = ress.data
              if (res.code === errcode) {
                getOrSetSessionVal('SET', 'footschildtype', JSON.stringify(res.data))
                resolve('成功')
              }
              reject('失败')
            }, () => {
              reject('失败')
            })
          }
        }).catch(err => {
          console.log(err)
        })
      },
      getfootsextractionmethodflag () {
        // 足迹物证提取方法
        return new Promise((resolve, reject) => {
          if (getOrSetSessionVal('GET', 'footsextractionmethod')) {
            resolve('成功')
          } else {
            getfootsextractionmethod((ress) => {
              const res = ress.data
              if (res.code === errcode) {
                getOrSetSessionVal('SET', 'footsextractionmethod', JSON.stringify(res.data))
                resolve('成功')
              }
              reject('失败')
            }, () => {
              reject('失败')
            })
          }
        }).catch(err => {
          console.log(err)
        })
      }
    }
  }
</script>
